﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace IUProjeto0
{
    public partial class Principal : Form
    {
        private Splash splash;
        private bool done = false;

        AC_Login _login = new AC_Login();

        public Principal()
        {
            InitializeComponent();
            _login.ShowDialog();

            if (_login.Autenticado == true)
            {
                this.Load += new EventHandler(HandleFormLoad);
                this.splash = new Splash();
            }
            else
            {
                MessageBox.Show("O Login falhou!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
        }

        private void HandleFormLoad (object sender, EventArgs e)
        { 
            this.Hide();
            Thread thread = new Thread(new ThreadStart(this.ShowSplashScreen));
            thread.Start();

            Inicializacao init = new Inicializacao();
            init.InicializacaoAvancou += (o, ex) => { this.splash.AtualizaPercentual(ex.Percentual); };
            init.InicializacaoCompletada += (o, ex) => { done = true; this.Show(); };
            init.executaInicializacao();
        }

        private void ShowSplashScreen()
        {
            splash.Show();
            while (!done) {
                Application.DoEvents();
            }
            splash.Close();
            this.splash.Dispose();

        }
        
        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Sobre s = new Sobre();
            s.Show();
        }
    }
}
